<?php

namespace Biz\service\BrandRemind;

use Doraemon\cache\Queue;
use Doraemon\tools\Sms;

class BrandLevelRemind
{

    //品牌商学院运营号
    const BRAND_COLLEGE_OP_UID = 12893279;

    //通知场景
    const REMIND_SCENE_ONLINE = 1; #上线当天对所有品牌运营者手机号进行发送
    const REMIND_SCENE_NOT_FINISH_TASK_6DAYS = 2; #当存在套餐的品牌，6天内没有完成任意任务时，进行提示。
    const REMIND_SCENE_BRAND_EXPIRE_29DAYS = 3; #当过期品牌达到29天时，进行提示

    //通知方式
    const REMIND_TYPE_SMS = 1; #短信
    const REMIND_TYPE_PUSH = 2; #消息推送
    const REMIND_TYPE_SITE_MESSAGE = 3; #站内消息

    //通知内容
    const REMIND_CONTENTS = [
        self::REMIND_SCENE_ONLINE => [
            self::REMIND_TYPE_SMS => [
                'msg' => '品牌成长体系全新上线，快登陆好好住APP或品牌后台查看吧，海量福利等你来领。',
                'param' => [],
                'tpl_ids' => [
                    'CnUnicom' => Sms::CHINA_UNICOM_CONFIG['temp_remind_brand_level_online'],
                    'aliyun' => Sms::TYPE_REMIND_BRAND_LEVEL_ONLINE
                ],
            ],
            self::REMIND_TYPE_PUSH => [
                'title' => '好好住品牌成长体系全新上线',
                'alert' => '超多权益等你解锁，快来领取吧。',
            ]
        ],
        self::REMIND_SCENE_NOT_FINISH_TASK_6DAYS => [
            self::REMIND_TYPE_SITE_MESSAGE => [
                'msg' => '您的成长值正在以5点/天的速度下降，完成任意任务停止下降，请尽快行动吧。',
                'from' => self::BRAND_COLLEGE_OP_UID,
            ],
            self::REMIND_TYPE_PUSH => [
                'title' => '品牌成长值正在下降',
                'alert' => '每天降低5点，完成任意任务停止下降，请尽快行动吧。',
            ],
        ],
        self::REMIND_SCENE_BRAND_EXPIRE_29DAYS => [
            self::REMIND_TYPE_SITE_MESSAGE => [
                'msg' => '您的成长值即将以200/天的速度下降，未使用的权益将会被清空，为避免损失，请及时续费。',
                'from' => self::BRAND_COLLEGE_OP_UID,
            ],
            self::REMIND_TYPE_SMS => [
                'msg' => "您的品牌成长值即将以{p}的速度下降，未使用的权益将会被清空，为避免损失，请及时续费。",
                'param' => [
                    'p' => '200/天',
                ],
                'tpl_ids' => [
                    'aliyun' => Sms::TYPE_REMIND_BRAND_EXPIRE_29DAYS,
                ],
            ],
            self::REMIND_TYPE_PUSH => [
                'title' => '品牌成长值将要大幅下降',
                'alert' => '将每天降低200点，为避免损失，请及时续费。',
            ],
        ],
    ];

    #提醒方式映射
    const REMINDERS = [
        self::REMIND_TYPE_SMS => BrandSms::class,
        self::REMIND_TYPE_PUSH => BrandPush::class,
        self::REMIND_TYPE_SITE_MESSAGE => BrandMessage::class,
    ];

    #执行提醒
    public function handle($scene, $uid, $phone = '')
    {
        //防止全发
        if($uid == 0){
            return false;
        }

        $brandRemind = new BrandRemind();
        if (!isset(self::REMIND_CONTENTS[$scene])) {
            return false;
        }
        foreach (self::REMIND_CONTENTS[$scene] as $remindType => $message) {
            $reminderName = self::REMINDERS[$remindType];
            $reminder = new $reminderName();
            $aParam = $this->buildParam($scene, $remindType, $uid, $phone);
            if (empty($aParam)) {
                continue;
            }
            $reminder->setParam($aParam);
            $brandRemind->add($reminder);
        }
        $brandRemind->notify();
        return true;
    }


    /**
     * 构造对应通知类型的通知参数
     * @param $scene
     * @param $remindType
     * @param $toUid
     * @param $toPhone
     * @return array
     * @throws \Exception
     */
    private function buildParam($scene, $remindType, $toUid, $toPhone = '')
    {
        $sceneMessage = $this->getSceneMessage($scene, $remindType);
        switch ($remindType) {
            case self::REMIND_TYPE_SMS:
                $aParam = $this->prepareSmsSendParam($sceneMessage, $toPhone);
                break;
            case self::REMIND_TYPE_PUSH:
                $aParam = $this->preparePushParam($scene, $sceneMessage, $toUid);
                break;
            case self::REMIND_TYPE_SITE_MESSAGE:
                $aParam = $this->prepareSiteMsgParam($sceneMessage, $toUid);
                break;
            default:
                throw new \Exception('remind type error!');
        }
        return $aParam;
    }

    /**
     * 准备短信发送参数
     * @param $sceneMessage
     * @param $toUid
     * @return array
     */
    private function prepareSmsSendParam($sceneMessage, $toPhone)
    {
        $aParam = [];
        if (empty($toPhone)) {
            return $aParam;
        }
        $aParam = [
            'iphone' => $toPhone,
            'aParam' => $sceneMessage['param'],
            'aTempIds' => $sceneMessage['tpl_ids'],
            'IDDCode' => '86',
        ];
        return $aParam;
    }

    /**
     * 准备PUSH发送参数
     * @param $scene
     * @param $sceneMessage
     * @param $toUid
     * @return array
     */
    private function preparePushParam($scene, $sceneMessage, $toUid)
    {
        $pushId = sprintf("%s_%d_%d_%s", 'brand_level_push', $scene, $toUid, date('YmdHis'));
        $aParam = [
            'uid' => $toUid,
            'title' => $sceneMessage['title'],
            'alert' => $sceneMessage['alert'],
            'param' => [
                'link' => '',
                'push_id' => $pushId,
                'image' => ''
            ]
        ];
        return $aParam;
    }

    /**
     * 准备站内信参数
     * @param $sceneMessage
     * @param $toUid
     * @return array
     */
    private function prepareSiteMsgParam($sceneMessage, $toUid)
    {
        $params = [
            'to_uids' => [$toUid],
            'msgInfo' => [
                'send_user' => $sceneMessage['from'],
                'con_type' => 0,
                'content' => json_encode([
                    'con' => $sceneMessage['msg'],
                ])
            ],
        ];

        $aParam = [
            'controller' => 'queue/im/send',
            'params' => $params,
            'queueName' => Queue::QUEUE_DORAEMON,
        ];

        return $aParam;
    }


    #获取通知场景内容
    public function getSceneMessage($scene, $remindType)
    {
        $remindContents = self::REMIND_CONTENTS[$scene];
        $message = $remindContents[$remindType];
        return $message;
    }

}